package io.chenglicun.modules.clc.service.dataset.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.chenglicun.common.page.PageData;
import io.chenglicun.common.service.impl.CrudServiceImpl;
import io.chenglicun.modules.clc.dao.dataset.ClcDataContentBakDao;
import io.chenglicun.modules.clc.dto.dataset.ClcDataContentBakDTO;
import io.chenglicun.modules.clc.dto.dataset.ClcDataContentDTO;
import io.chenglicun.modules.clc.entity.dataset.ClcDataContentBakEntity;
import io.chenglicun.modules.clc.service.dataset.ClcDataContentBakService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据内容删除备份
 *
 * @author HFHQ 137664951@qq.com
 * @since 1.0 2024-04-15
 */
@Service
public class ClcDataContentBakServiceImpl extends CrudServiceImpl<ClcDataContentBakDao, ClcDataContentBakEntity, ClcDataContentBakDTO> implements ClcDataContentBakService {

    @Override
    public QueryWrapper<ClcDataContentBakEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<ClcDataContentBakEntity> wrapper = new QueryWrapper<>();


        return wrapper;
    }

    @Override
    public PageData<ClcDataContentBakDTO> getPage(Map<String, Object> params) {
        System.out.println(params);
        // 获取行号
        if (params.get("setId") == null && StringUtils.isEmpty(params.get("setId").toString())) {
            return new PageData<>(null, 0);
        }
        Integer page = 1;
        Integer limit = 10;
        if (params.get("page") != null && StringUtils.isNotEmpty(params.get("page").toString())) {
            page = Integer.valueOf(params.get("page").toString());
        }
        if (params.get("limit") != null && StringUtils.isNotEmpty(params.get("limit").toString())) {
            limit = Integer.valueOf(params.get("limit").toString());
        }

        Integer limitStart = page * limit - limit;
        Integer limitEnd = limitStart + limit;
        QueryWrapper<ClcDataContentBakEntity> pageIdsWrapper = new QueryWrapper<>();
        pageIdsWrapper.lambda().select(ClcDataContentBakEntity::getRowNum).eq(ClcDataContentBakEntity::getSetId, params.get("setId")).groupBy(ClcDataContentBakEntity::getRowNum).last("limit " + limitStart + "," + limitEnd);
        QueryWrapper<ClcDataContentBakEntity> queryWrapperCount = new QueryWrapper<>();
        queryWrapperCount.select("count(row_num)");
        queryWrapperCount.lambda().select(ClcDataContentBakEntity::getRowNum).eq(ClcDataContentBakEntity::getSetId, params.get("setId")).groupBy(ClcDataContentBakEntity::getRowNum);
        if (params.get("keyStr") != null && StringUtils.isNotEmpty(params.get("keyStr").toString())) {
            pageIdsWrapper.lambda().like(ClcDataContentBakEntity::getValue, params.get("keyStr"));
            queryWrapperCount.lambda().like(ClcDataContentBakEntity::getValue, params.get("keyStr"));
        }
        List<ClcDataContentBakEntity> clcDataContentEntities = baseDao.selectList(pageIdsWrapper);

        List<Map<String, Object>> maps = baseDao.selectMaps(queryWrapperCount);
        Integer total = maps.size();
        if (clcDataContentEntities == null || clcDataContentEntities.size() <= 0)
            return new PageData<>(null, 0);
        List<Long> rowNums = new ArrayList<>();
        for (ClcDataContentBakEntity rowNum : clcDataContentEntities) {
            rowNums.add(rowNum.getRowNum());
        }
        List<ClcDataContentBakEntity> list = baseDao.selectList(new QueryWrapper<ClcDataContentBakEntity>().lambda().eq(ClcDataContentBakEntity::getSetId, params.get("setId")).in(ClcDataContentBakEntity::getRowNum, rowNums));
        // 以行号分组
        Map<Long, List<ClcDataContentBakEntity>> map = new HashMap<>();
        for (ClcDataContentBakEntity l : list) {
            if (!map.containsKey(l.getRowNum())) {
                map.put(l.getRowNum(), new ArrayList<>());
            }
            map.get(l.getRowNum()).add(l);
        }
        List<List<ClcDataContentBakEntity>> data = new ArrayList<>();
        for (Long k : map.keySet()) {
            data.add(map.get(k));
        }
        PageData pageData = new PageData(data, total);
        return pageData;
    }

}